package com.blb.zycommunity.controller;

import com.blb.zycommunity.config.AutoLog;
import com.blb.zycommunity.entity.*;
import com.blb.zycommunity.service.IZyCommunityService;
import com.blb.zycommunity.utils.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.ZyCommunity;
import com.blb.zycommunity.service.ISysAreaService;
import com.blb.zycommunity.utils.TreeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 小区  前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
@CrossOrigin
public class ZyCommunityController {

    @Autowired
    private IZyCommunityService zyCommunityService;

    @GetMapping("/community")
    public ResponseResult<List<ZyCommunity>> findCommunitys(){
        List<ZyCommunity> communities = zyCommunityService.list();
        return ResponseResult.ok(communities);
    }

    @Autowired
    private ISysAreaService iSysAreaService;

    @GetMapping("/community-pages")
    public ResponseResult<IPage<ZyCommunity>> findCommunityPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                                @RequestParam(required = false,defaultValue = "5") Long size,
                                                                @RequestParam(required = false,defaultValue = "") String name,
                                                                @RequestParam(required = false,defaultValue = "") String code,
                                                                @RequestParam(required = false,defaultValue = "") String id){
        Page<ZyCommunity> zyCommunityPage = new Page<>(current, size);
        IPage<ZyCommunity> page = zyCommunityService.findCommunityPage(zyCommunityPage,name,code,id);
        return ResponseResult.ok(page);
    }

//    获取所有数据转成树
    @GetMapping("/area-get")
    public ResponseResult<List<SysArea>> findAreaAll(){
        List<SysArea> areas = zyCommunityService.findAll();
        List<SysArea> areaTree = TreeUtils.areasToTree(areas);
        return ResponseResult.ok(areaTree);
    }

    @AutoLog(value = 1,title = "小区新增")
    @PostMapping("/community-add")
    public ResponseResult addCommunity(@RequestBody ZyCommunity community){
        boolean save = zyCommunityService.save(community);
        return ResponseResult.ok(save);
    }

    @AutoLog(value = 2,title = "小区修改")
    @PutMapping("/community-edit")
    public ResponseResult editCommunity(@RequestBody ZyCommunity community){
        boolean update = zyCommunityService.updateById(community);
        return ResponseResult.ok(update);
    }

    @AutoLog(value = 3,title = "小区删除")
    @DeleteMapping("/community-delete/{communityId}")
    public ResponseResult deleteCommunity(@PathVariable String communityId){
        boolean removeById = zyCommunityService.removeById(communityId);
        return ResponseResult.ok(removeById);
    }


    @DeleteMapping("/communities-delete/{communityIds}")
    public ResponseResult deleteCommunityByIds(@PathVariable Long[] communityIds){
        for (Long communityId : communityIds) {
            zyCommunityService.removeById(communityId);
        }
        return ResponseResult.ok("ok");
    }

    /**
     * 根据id导出数据
     * @param response
     * @param ids
     * @param filename
     * @throws IOException
     */
    @AutoLog(value = 6,title = "小区信息导出")
    @GetMapping("downloads-community")
    public void exports(
            HttpServletResponse response,
            @RequestParam("ids") long[] ids,
            @RequestParam("filename") String filename) throws IOException {
        System.out.println(filename);
        List<ZyCommunity> zyCommunities = new ArrayList<>();
        for (Long id : ids) {
            String idstr = id.toString();
            ZyCommunity zyCommunity = zyCommunityService.selectCommunityById(idstr);
            zyCommunities.add(zyCommunity);
        }
        ExcelDownloadUtil.excelDownload(response, filename, ZyCommunity.class, zyCommunities);
    }


}
